package com.education.wisdom.controller.management;


import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.Resources;
import com.education.wisdom.service.ICatalogService;
import com.education.wisdom.service.IResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/manage")
public class CatalogController {

    @Autowired
    private ICatalogService catalogService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IResourcesService resourcesService;

    /**
     * 查询目标目录名称
     */
    @GetMapping("/selectByName")
    public String selectByName(Integer id) {
        Catalog catalog = catalogService.selectByCatalogId2(id);
        String name = catalog == null ? "" : catalog.getName();
        return name;
    }

    /**
     * 根据catalogId删除
     */
    @DeleteMapping("/delete")
    public ApiReturnObject delete(Integer catalogId) {
        //判断该目录是否有子目录 有则不能删除
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        if (list.size() > 0) {
            return new ApiReturnObject("201", "该目录有子目录 请先删除子目录", "该目录有子目录 请先删除子目录");
        }
        Resources resources = new Resources();
        resources.setrCatalogid(catalogId);
        List<Resources> list1 = resourcesService.selectResourcesList(resources);
        if (list1.size() > 0) {
            return new ApiReturnObject("202", "该课程下有资源 无法删除", "该课程下有资源 无法删除");
        }
        int i = catalogService.deleteByCatalogId2(catalogId);
        redisTemplate.delete("catalogMenu");
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }


    /**
     * 修改
     */
    @PutMapping("/update")
    public ApiReturnObject update(@RequestBody Catalog catalog) {
        redisTemplate.delete("catalogMenu");
        catalog.setUpdateTime(new Date());
//        if (catalog.getParentId() != null) {
//            if (getInt(catalog.getCatalogId())) {
//                return new ApiReturnObject("201", "failure", "目录修改异常");
//            }
//        }
        catalogService.updateCataLog(catalog);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }


    /**
     * 批量修改
     *
     * @return
     */
    @PutMapping("/batchUpdate")
    public ApiReturnObject batchUpdate(@RequestBody Catalog catalog) {
        redisTemplate.delete("catalogMenu");
        catalog.setUpdateTime(new Date());
        for (Integer catalogId : catalog.getCatalogIds()) {
            catalog.setCatalogId(catalogId);
            catalogService.updateCataLog(catalog);
        }
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }

    //该目录如果添加到自己及其子目录下 则不能添加
    public boolean getInt(Integer catalogId) {
        List<Integer> list = new ArrayList<>();
        list.add(catalogId);
        recursiveInt(catalogId, list);
        for (Integer integer : list) {
            if (catalogId == integer) {
//                throw new BusinessException(CommonErrorCode.E_200111);
                return true;
            }
        }
        return false;
    }

    //递归查询所有课程id
    public void recursiveInt(Integer catalogId, List<Integer> list) {
        List<Catalog> list1 = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : list1) {
            list.add(catalog.getCatalogId());
            if (isNext(catalog.getCatalogId())) {
                recursiveInt(catalog.getCatalogId(), list);
            }
        }
    }

    /**
     * 新增接口
     */
    @PostMapping("/insert")
    public ApiReturnObject insert(@RequestBody Catalog catalog) {
        redisTemplate.delete("catalogMenu");
        catalog.setCreateTime(new Date());
        catalogService.insertCatalog(catalog);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }


    /**
     * 搜索接口
     */
    @GetMapping("/search")
    public ApiReturnObject search(Catalog catalog) {
        List<Catalog> list = catalogService.selectCatalogList2(catalog);
        return new ApiReturnObject("200", "操作成功", list);
    }


    @GetMapping("/listAll")
    public ApiReturnObject listAll(Integer catalogId) {
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        recursive(list);
        return new ApiReturnObject("200", "操作成功", list);
    }


    /**
     * 平台目录管理--专业课程体系表
     * 递归查询
     *
     * @return
     */
    @GetMapping("/list")
    public ApiReturnObject list() {
        //当redis不为空 直接从redis拿数据
        List redisCatalogs = (List) redisTemplate.opsForValue().get("catalogList");
        if (redisCatalogs != null) {
            return new ApiReturnObject("200", "操作成功", redisCatalogs);
        }

        List<Catalog> list = catalogService.selectByParentId(0);
        recursive(list);
        redisTemplate.opsForValue().set("catalogList", list);
        return new ApiReturnObject("200", "操作成功", list);
    }

    /**
     * 平台目录管理--专业课程体系表
     *
     * @return
     */
    @GetMapping("/list2")
    public ApiReturnObject list2() {
        Object catalogMenu = redisTemplate.opsForValue().get("catalogMenu");
        List<Catalog> catas = null;
        if (Objects.nonNull(catalogMenu)) {
            catas = (List<Catalog>) catalogMenu;
        }
        //List<Catalog> catas = (List<Catalog>) redisTemplate.opsForValue().get("catalogMenu");
        if (StringUtils.isNotEmpty(catas)) {
            return new ApiReturnObject("200", "操作成功", catas);
        }
        List<Catalog> list = catalogService.selectByParentId(0);
        for (Catalog catalog : list) {
            List<Catalog> catalogs = catalogService.selectByParentId(catalog.getCatalogId());
            catalog.setChildren(catalogs);
            for (Catalog cata : catalogs) {
                List<Catalog> catalogList = catalogService.selectByParentId(cata.getCatalogId());
                cata.setChildren(catalogList);
                if (isPre(cata.getParentId())) {
                    Catalog ca = catalogService.selectByCatalogId2(cata.getParentId());
                    cata.setParentName(ca.getName());
                }
                for (Catalog c : catalogList) {
                    if (isPre(c.getParentId())) {
                        Catalog ca = catalogService.selectByCatalogId2(c.getParentId());
                        c.setParentName(ca.getName());
                    }
                }
            }
        }
        redisTemplate.opsForValue().set("catalogMenu", list);
        return new ApiReturnObject("200", "操作成功", list);
    }

    //递归查询课程
    public void recursive(List<Catalog> catalogs) {
        for (Catalog catalog : catalogs) {
            if (isPre(catalog.getParentId())) {
                Catalog catalog1 = catalogService.selectByCatalogId2(catalog.getParentId());
                catalog.setParentName(catalog1.getName());
            }
            if (isNext(catalog.getCatalogId())) {
                List<Catalog> list1 = catalogService.selectByParentId(catalog.getCatalogId());

                catalog.setChildren(list1);
                recursive(list1);
            }
        }
    }


    //查询课程是否有下一级
    public boolean isNext(Integer catalogId) {
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        if (list.size() == 0) {
            return false;
        } else {
            return true;
        }
    }

    //查询课程是否有上一级
    public boolean isPre(Integer catalogId) {
        List<Catalog> list = catalogService.selectByCatalogId(catalogId);
        if (list.size() == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 加载目录列表树
     */
    @GetMapping("/treeData")
    public List<Map<String, Object>> treeData() {
        List<Map<String, Object>> tree = catalogService.selectCatalogTree();
        return tree;
    }

///////////////////////////////////////////////////////////////////////////////////////
////////////////////////**********资源管理************//////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

    //查询
    @GetMapping("/Resource/SelectAll")
    public ApiReturnObject selectAll(Resources resources, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Resources> resourcesList = resourcesService.selectResourcesList(resources);
        for (Resources res : resourcesList) {
            if (res.getrCatalogid() != null) {
                Catalog catalog = catalogService.selectByCatalogId2(res.getrCatalogid());
                if (catalog != null) {
                    res.setParentName(catalog.getName());
                }
            }
        }
        PageInfo<Resources> pageInfo = new PageInfo<>(resourcesList);
        return new ApiReturnObject("200", "操作成功", pageInfo);
    }

    //删除
    @GetMapping("/Resource/Delete")
    public ApiReturnObject deleteResourceById(String id) {
        resourcesService.deleteResourcesByIds(id);
        return new ApiReturnObject("200", "操作成功", "删除成功");
    }

    //添加
    @PostMapping("/Resource/ins")
    public ApiReturnObject insResources(@RequestBody Resources resources) {
        resources.setCreateTime(new Date());
        resourcesService.insertResources(resources);
        return new ApiReturnObject("200", "操作成功", "添加成功");
    }


    //修改
    @PostMapping("/Resource/update")
    public ApiReturnObject updateResources(@RequestBody Resources resources) {
        resourcesService.updateResources(resources);
        return new ApiReturnObject("200", "操作成功", "修改成功");
    }


}
